package com.song.EMS.controller;

import com.song.EMS.bean.Message;
import com.song.EMS.bean.ResultData;
import com.song.EMS.bean.User;
import com.song.EMS.mvc.ResponseBody;
import com.song.EMS.mvc.ResponseView;
import com.song.EMS.service.impl.UserServiceImpl;
import com.song.EMS.utils.DateFormatUtil;
import com.song.EMS.utils.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class UserController {
    private UserServiceImpl userService = new UserServiceImpl();


    /**
     * 查询所有用户信息
     * @param request
     * @param response
     * @return 查询到的数据信息返回给前端显示
     */
    @ResponseBody("/user/selectAll.do")
    public String selectAll(HttpServletRequest request, HttpServletResponse response){
        //获取前端信息
        String pageIndex = request.getParameter("pageIndex");
        String pageSize = request.getParameter("pageSize");
        //调用service
        List<User> users = userService.selectUsers(true, Integer.parseInt(pageIndex), Integer.parseInt(pageSize));

        int total = userService.selectUsers(false, 0, 0).size();
        System.out.println(users.size() + "======" +total);
        //将查询到的数据分装为bootstrap-table可用识别的格式
        ResultData<User> data = new ResultData<>();
        data.setRows(users);
        data.setTotal(total);
        return JsonUtil.toJSON(data);

    }

    /**
     * 添加用户
     * @param request
     * @param response
     * @return Json格式的新添用户数据
     */
    @ResponseBody("/user/insert.do")
    public String insertUser(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        //获取前端数据
        String userName = request.getParameter("userName");
        String userGender = request.getParameter("userGender");
        int gender = (userGender == null || userGender.equals("")) ?1:Integer.parseInt(userGender);
        System.out.println(userGender);
        String IDCard = request.getParameter("IDCard");
        String password = request.getParameter("password");
        String userPhone = request.getParameter("userPhone");

        User user = new User(userName,IDCard,gender,password,userPhone);
        System.out.println(user);
        //调用service
        Integer integer = userService.insertUser(user);
        if (integer == 1){
            data = new Message(1,"添加成功");
        }else if (integer == 0){
            data = new Message(0,"用户名已注册");
        }else if (integer == -1){
            data = new Message(-1,"电话号码已被注册");
        }else {
            data = new Message(-2,"添加失败");
        }
        //转换为Json，返回给前端
        return JsonUtil.toJSON(data);

    }

    /**
     * 通过用户手机号查询用户
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/user/select.do")
    public String selectByPhone(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        String userPhone = request.getParameter("userPhone");
        System.out.println(userPhone);
        User user = userService.selectByPhone(userPhone, null);
        System.out.println(user);
        if(user==null){
            data = new Message(0,"号码不存在");
        }else {
            data = new Message(1,"查询成功",user);
        }
        System.out.println(data.getStatus());
        return JsonUtil.toJSON(data);
    }

    /**
     * 修改用户信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/user/update.do")
    public String updateUser(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        //获取前端数据
        String userId = request.getParameter("userId");
        String userName = request.getParameter("userName");
        String userGender = request.getParameter("userGender");
        int gender = (userGender == null || userGender.equals("")) ?1:Integer.parseInt(userGender);
        String IDCard = request.getParameter("IDCard");
        String password = request.getParameter("password");
        String userPhone = request.getParameter("userPhone");

        User user = new User(Integer.parseInt(userId),userName,IDCard,gender,password,userPhone);
        Integer integer = userService.updateUser(user);
        if (integer == 1){
            data = new Message(1,"修改成功");
        }else if (integer == 0){
            data = new Message(0,"用户名已注册");
        }else if (integer == -1){
            data = new Message(-1,"电话号码已被注册");
        }else {
            data = new Message(-2,"修改失败");
        }
        //转换为Json，返回给前端
        return JsonUtil.toJSON(data);
    }

    /**
     * 删除用户
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/user/delete.do")
    public String deleteUser(HttpServletRequest request, HttpServletResponse response){
        Message data = null;
        String userId = request.getParameter("userId");

        Boolean aBoolean = userService.deleteUser(Integer.parseInt(userId));
        if (aBoolean){
            data = new Message(1,"删除成功");
        }else {
            data = new Message(0,"删除失败");
        }
        return JsonUtil.toJSON(data);


    }

    /**
     * 控制台中的数据
     * @param request
     * @param response
     * @return 给前端的用户数据
     */
    @ResponseBody("/admin/views/user-console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        //调用service
        Message data = null;
        List userList = userService.userConsole();

        if(userList.size() >0){
            data = new Message(1,null,userList);

        }else {
            data = new Message(-1,null);
        }

        return JsonUtil.toJSON(data);
    }
}
